package com.example.elmmerchants.Product_Management;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;

import java.util.ArrayList;
import java.util.List;

public class Setting_Specification_Page extends AppCompatActivity {

    LinearLayout specification_item, add_the_specification, is_specification, specification_information, current;
    TextView current_text_view;

    int how_many_specification = 0;
    boolean has_specification = false;
    List<Specification_List> specification = new ArrayList<>();
    ArrayList<String> intent_data = new ArrayList<>();
    boolean is_all_input = true;

    private class View_Thread extends Thread {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    current.setBackgroundResource(R.drawable.border_yellow);
                    current_text_view.setTextColor(Color.BLACK);
                    Specification_List s = new Specification_List();
                    how_many_specification += 1;
                    is_specification.setVisibility(View.VISIBLE);

                    View view2 = LayoutInflater.from(Setting_Specification_Page.this).inflate(R.layout.specification_information_view, null);
                    TextView specification_name = view2.findViewById(R.id.specification_name);
                    EditText set_product_price = view2.findViewById(R.id.set_product_price);
                    EditText set_inventory = view2.findViewById(R.id.set_inventory);
                    Switch is_open_auto_re_inventory = view2.findViewById(R.id.is_open_auto_re_inventory);
                    Switch is_sold = view2.findViewById(R.id.is_sold);
                    EditText weight = view2.findViewById(R.id.weight);
                    specification_information.addView(view2);

                    View view1 = LayoutInflater.from(Setting_Specification_Page.this).inflate(R.layout.set_specification_view, null);
                    ImageView delete = view1.findViewById(R.id.delete);
                    EditText set_specification_name = view1.findViewById(R.id.specification_name);
                    set_specification_name.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            specification_name.setText(set_specification_name.getText().toString());
                        }
                    });
                    delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            specification_item.removeView(view1);
                            how_many_specification -= 1;
                            for (int i = 0; i < specification.size(); i++) {
                                if (view1 == specification.get(i).view) {
                                    specification.remove(i);
                                    break;
                                }
                            }
                            specification_information.removeView(view2);
                            if (how_many_specification == 0) {
                                is_specification.setVisibility(View.GONE);
                                current.setBackgroundResource(R.drawable.border_gray_10dp);
                                current_text_view.setTextColor(Color.rgb(128, 128, 128));
                            }
                        }
                    });
                    specification_item.addView(view1);
                    s.view = view1;
                    s.is_sold = is_sold;
                    s.set_specification_name = set_specification_name;
                    s.set_inventory = set_inventory;
                    s.set_product_price = set_product_price;
                    s.is_open_auto_re_inventory = is_open_auto_re_inventory;
                    s.weight = weight;
                    specification.add(s);
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_specification_page);

        specification_item = findViewById(R.id.specification_item);
        add_the_specification = findViewById(R.id.add_the_specification);
        is_specification = findViewById(R.id.is_specification);
        specification_information = findViewById(R.id.specification_information);
        current = findViewById(R.id.current);
        current_text_view = findViewById(R.id.current_text_view);

        add_the_specification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View_Thread viewThread = new View_Thread();
                viewThread.start();
            }
        });

        current.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (specification.size() > 0) {
                    is_all_input = true;
                    intent_data.clear();
                    for (int i = 0; i < specification.size(); i++) {
                        if (specification.get(i).set_specification_name.getText().toString().isEmpty() || specification.get(i).set_product_price.getText().toString().isEmpty() || specification.get(i).set_inventory.getText().toString().isEmpty() || specification.get(i).weight.getText().toString().isEmpty()) {
                            is_all_input = false;
                            break;
                        }
                        intent_data.add(specification.get(i).set_specification_name.getText().toString());
                        intent_data.add(specification.get(i).set_product_price.getText().toString());
                        intent_data.add(specification.get(i).set_inventory.getText().toString());
                        intent_data.add(specification.get(i).weight.getText().toString());
                        if (specification.get(i).is_open_auto_re_inventory.isChecked()) {
                            intent_data.add("1");
                        }
                        else {
                            intent_data.add("0");
                        }
                        if (specification.get(i).is_sold.isChecked()) {
                            intent_data.add("1");
                        }
                        else {
                            intent_data.add("0");
                        }
                    }

                    if (is_all_input) {
                        Intent intent = new Intent(Setting_Specification_Page.this, Create_Product_Page.class);
                        intent.putStringArrayListExtra("specification_data", intent_data);
                        setResult(1, intent);
                        finish();
                    }
                    else {
                        Toast.makeText(Setting_Specification_Page.this, "请输入所有数据", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(Setting_Specification_Page.this, "请设置规格", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}